package de.mroedig.appindiesonne.menu;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
import de.mroedig.appindiesonne.CountDownStates;
import de.mroedig.appindiesonne.R;

public class Ergebnis extends Activity {

	private final class CountDownTimerImpl extends CountDownTimer {
		public int getMiliseconds() {
			return this.miliseconds;
		}

		public void setMiliseconds(int miliseconds) {
			this.miliseconds = miliseconds;
		}

		private int miliseconds;

		private CountDownTimerImpl(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);
		}

		@Override
		public void onTick(long millisUntilFinished) {
			long seconds = (millisUntilFinished / 1000) % 60;
			long minutes = ((millisUntilFinished / (1000 * 60)) % 60);
			long hours = ((millisUntilFinished / (1000 * 60 * 60)) % 24);

			this.miliseconds = (int) millisUntilFinished;

			Ergebnis.this.timePicker.setCurrentMinute((int) minutes);
			Ergebnis.this.timePicker.setCurrentHour((int) hours);

			Ergebnis.this.ergebnisInMinuten.setText("Sonnenbaddauer: " + hours
					+ ":" + minutes + ":" + seconds);
		}

		@Override
		public void onFinish() {
			Ergebnis.this.ergebnisInMinuten.setText("App In Den Schatten!");
			String ns = Context.NOTIFICATION_SERVICE;
			NotificationManager mNotificationManager = (NotificationManager) Ergebnis.this
					.getSystemService(ns);
			final int HELLO_ID = 1;

			Notification note = new Notification();
			note.defaults |= Notification.DEFAULT_SOUND;
			long[] vibrate = { 200, 100, 200, 300, };
			note.vibrate = vibrate;
			mNotificationManager.notify(HELLO_ID, note);
			Ergebnis.this.sichereMilisekunden(this.miliseconds);
		}
	}

	private static final int EIGENSCHUTZFAKTOR = 5;
	private TextView ergebnisInMinuten;
	private TimePicker timePicker;
	private Button start;
	private CountDownTimer countDownTimer;
	private double erg = 0;
	private CountDownStates state;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.ergebnis);
		this.ergebnisInMinuten = (TextView) this
				.findViewById(R.id.ergebnisInMinuten);
		this.timePicker = (TimePicker) this.findViewById(R.id.countDown);
		this.start = (Button) this.findViewById(R.id.startButton);
		this.dauerBerechnen();
		this.state = CountDownStates.INIT;
		Ergebnis.this.ergebnisInMinuten.setText(R.string.sonnenbadrestdauer
				+ ": " + this.zeigeZeitstempel());
		this.sichereMilisekunden((int) (this.erg * 1000 * 60));
		this.start.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				switch (Ergebnis.this.state) {
				case INIT:
					Ergebnis.this.startCountdown();
					break;
				case STOPPED:
					Ergebnis.this.startCountdown();
					break;
				case PAUSED:
					Ergebnis.this.wakeupCountdown();
					break;

				case STARTED:
					Ergebnis.this.pauseCountdown();
					break;

				}
			}
		});

	}

	private String zeigeZeitstempel() {
		int seconds = (int) (((this.erg * 60) % 60));
		int minutes = (int) (this.erg) % 60;
		int hours = ((int) this.erg / (60)) % 24;
		String sekundenfueller = "";
		String minutenfueller = "";
		if (seconds < 10) {
			sekundenfueller = "0";
		}
		if (minutes < 10) {
			minutenfueller = "0";
		}
		return hours + ":" + minutenfueller + minutes + ":" + sekundenfueller
				+ seconds;
	}

	public void startCountdown() {
		this.state = CountDownStates.STARTED;
		this.start.setText(R.string.pausebutton);
		int miliseconds;
		SharedPreferences mPrefs = this.getSharedPreferences(
				"appInDieSonnePrefs", MODE_PRIVATE);
		if (mPrefs.getInt("milisecs", 0) != 0) {
			miliseconds = mPrefs.getInt("milisecs", 0);
		} else {
			miliseconds = (int) (this.erg * 60 * 1000);
		}

		if ((miliseconds % 60000) == 0) {
			this.sichereMilisekunden(miliseconds);
		}

		this.countDownTimer = new CountDownTimerImpl(miliseconds, 1000).start();

	}

	private void sichereMilisekunden(int miliseconds) {
		SharedPreferences mPrefs = this.getSharedPreferences(
				"appInDieSonnePrefs", MODE_PRIVATE);
		Editor editor = mPrefs.edit();

		editor.putInt("milisecs", miliseconds);
		editor.commit();
	}

	public void pauseCountdown() {
		this.sichereMilisekunden(((CountDownTimerImpl) this.countDownTimer)
				.getMiliseconds());
		this.countDownTimer.cancel();
		this.state = CountDownStates.PAUSED;
		this.start.setText(R.string.resumebutton);

	}

	public void wakeupCountdown() {
		this.start.setText(R.string.pausebutton);
		this.state = CountDownStates.STARTED;
		SharedPreferences mPrefs = this.getSharedPreferences(
				"appInDieSonnePrefs", MODE_PRIVATE);

		this.countDownTimer = new CountDownTimerImpl(mPrefs.getInt("milisecs",
				0), 1000);
		this.countDownTimer.start();

	}

	public void stopCountdown() {
		this.state = CountDownStates.STOPPED;
		this.countDownTimer.onFinish();
		this.resetCountDown();
	}

	public void resetCountDown() {
		this.state = CountDownStates.INIT;
		this.countDownTimer.onFinish();
		this.countDownTimer.cancel();
		this.dauerBerechnen();
		this.sichereMilisekunden((int) (this.erg * 1000));
	}

	private void dauerBerechnen() {

		this.berechneHauttypFaktor();
		this.berechneZonenFaktor();
		this.berechneZeitFaktor();
		this.berechneSonnenSchutzMitSonnencreme();

	}

	private void berechneSonnenSchutzMitSonnencreme() {
		SharedPreferences mPrefs = this.getSharedPreferences(
				"appInDieSonnePrefs", MODE_PRIVATE);
		int schutzfaktor = mPrefs.getInt("sonnenschutz", 0);
		if (schutzfaktor == 0) {
			schutzfaktor = 1;
		}
		this.erg = (this.erg * schutzfaktor) * 0.6;
	}

	private void berechneZeitFaktor() {
		SharedPreferences mPrefs = this.getSharedPreferences(
				"appInDieSonnePrefs", MODE_PRIVATE);
		double zwischen = 0;
		if ((mPrefs.getInt("stunde", 1) < 10)
				|| (mPrefs.getInt("stunde", 1) > 15)) {
			zwischen = this.erg;
		}
		if ((mPrefs.getInt("monat", 1) < 6) || (mPrefs.getInt("monat", 1) > 7)) {
			zwischen = zwischen + this.erg;
		}
		this.erg = this.erg + zwischen;
	}

	private void berechneZonenFaktor() {
		SharedPreferences mPrefs = this.getSharedPreferences(
				"appInDieSonnePrefs", MODE_PRIVATE);

		double zonenfaktor;
		switch (mPrefs.getInt("zone", 1)) {
		case 0:
			zonenfaktor = 10;
			break;
		case 1:
			zonenfaktor = 5;
			break;
		case 2:
			zonenfaktor = 0;
			break;
		case 3:
			zonenfaktor = -2.5;
			break;
		case 4:
			zonenfaktor = 0;
			break;
		case 5:
			zonenfaktor = 5;
			break;
		case 6:
			zonenfaktor = 10;
			break;
		default:
			zonenfaktor = 0;
		}

		this.erg = this.erg + zonenfaktor;

	}

	private void berechneHauttypFaktor() {
		SharedPreferences mPrefs = this.getSharedPreferences(
				"appInDieSonnePrefs", MODE_PRIVATE);
		this.erg = EIGENSCHUTZFAKTOR * mPrefs.getInt("hauttyp", 1);
	}
}
